<?php
$url = $_SERVER['REQUEST_URI'];
if (!preg_match('/\?(.*)/', $url, $matches)) {
    die('没有查询字符串');
}
require('../../cofd/common.php');
$getids = $matches[1];
$stmt = $conn->prepare("SELECT score,aname,ifzb FROM exam_ans WHERE lisid = ?");
if ($stmt === false) {
    echo json_encode(['status' => 500, 'msg' => '准备查询失败: ' . $mysqli->error]);
    exit();
}
$stmt->bind_param('s', $getids);
$stmt->execute();
$result = $stmt->get_result();
$userScore = null;
if ($result->num_rows > 0) {
    $row2 = $result->fetch_assoc();
    if($row2['score'] == null || $row2['score'] == ''){
        die('请耐心等待成绩公布(ERROR: 1062 试卷未批阅');
    }
    $userScore = explode(',', $row2['score']); 
    $userScore = array_sum($userScore); 
}else{
  die('没有查询到成绩');
}
$stmt->close();
$stmt = $conn->prepare("SELECT score FROM exam_ans");
if ($stmt === false) {
    echo json_encode(['status' => 500, 'msg' => '准备查询失败: ' . $mysqli->error]);
    exit();
}
$stmt->execute();
$result = $stmt->get_result();
$allScores = [];
while ($row = $result->fetch_assoc()) {
    $scores = explode(',', $row['score']);
    $totalScore = array_sum($scores);
    $allScores[] = $totalScore;
}
$stmt->close();
rsort($allScores); 
$rank = array_search($userScore, $allScores) + 1; 
$status = json_encode([
    'status' => 200,
    'msg' => '查询成功',
    'userScore' => $userScore,
    'rank' => $rank,
    'totalScores' => $allScores 
]);
if($row2['ifzb'] == 0){
  $ifelzb = '未作弊';
}else{
  $ifelzb = '作弊';
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>分数查询</title>
<style>
 body {
    font-family: 'Roboto', sans-serif;
    background-color: #f8f9fa;
    margin: 0;
    padding: 20px;
    color: #333;
}

.container {
    max-width: 800px;
    margin: 40px auto;
    background: #fff;
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

h2 {
    text-align: center;
    font-weight: 300;
    margin-bottom: 30px;
    color: #5c5c5c;
}

.score-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 30px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.score-table th,
.score-table td {
    border: 1px solid #e9ecef;
    padding: 12px 16px;
    text-align: left;
    transition: background-color 0.3s ease;
}

.score-table th {
    background-color: #e9ecef;
    font-weight: 500;
}

.score-table tr:hover {
    background-color: #f1f3f5;
}

.score-table th:first-child,
.score-table td:first-child {
    border-radius: 10px 0 0 10px;
}

.score-table th:last-child,
.score-table td:last-child {
    border-radius: 0 10px 10px 0;
}


.button {
    display: inline-block;
    background-color: #007bff;
    color: #fff;
    padding: 10px 20px;
    border-radius: 5px;
    font-size: 16px;
    text-decoration: none;
    transition: background-color 0.3s ease;
}

.button:hover {
    background-color: #0056b3;
}

.icon {
    margin-right: 8px;
}


@media (max-width: 768px) {
    .container {
        margin: 20px;
        padding: 20px;
    }
}
</style>
</head>
<body>
  <div class="container">
    <h2>分数查询结果</h2>
    <table class="score-table">
      <thead>
        <tr>
          <th>考生姓名</th>
          <th>试卷总分</th>
          <th>是否作弊</th>
          <th>最后得分</th>
          <th>得分占比</th>
          <th>整体排名</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><?php echo $row2['aname'];?></td>
          <td> <?php echo $scores[1] ?> </td>
          <td><?php echo $ifelzb ?></td>
          <td><?php echo $scores[0] ?></td>
          <td><?php echo round($scores[0]/$scores[1]*100,2) ?>%</td>
          <td><?php echo $rank ?></td>
        </tr>
      </tbody>
    </table>
  </div>
</body>
</html>